<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\core\Controller;
use herosphp\http\HttpRequest;
use herosphp\session\Session;
use herosphp\utils\AjaxResult;

/**
 * login action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class LoginAction extends Controller {

    protected $serviceBean = 'admin.manager.service';

    //登陆页面
    public function index(HttpRequest $request) {
        $service = Beans::get($this->serviceBean);
        //Session::start();

        $c = $request->getParameter('c');

        if(strlen($c)<50){

            $this->assign("c", $c);
        }

        $this->valiAdminEntryCode($c);


        //是否开启验证码验证

        $settingService = Beans::get('system.setting.service');
        $verifyCodeEnable = $settingService->getSetting('verifyCode');
        $this->assign('verifyCodeEnable',$verifyCodeEnable);

        if ( $service->isLogin() ) {
            location('/admin/manager/index');
        }
        $this->setView("system/login");
    }

    //验证入口码
    public function valiAdminEntryCode($c){

        Session::start();

        if(getConfig('admin_entry_vali')){

            if(getConfig('admin_entry_code')){

                if(getConfig('admin_entry_code')!=$c){
                    if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
                        AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '登陆失败,入口码错误!');
                    }else{
                        echo "<h1>Access Deny!</h1>";
                        die;
                    }

                }
            }else{

                AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '登陆失败,入口码错误!');
            }

        }


   }
    //登陆验证
    public function signin(HttpRequest $request) {

        $c = $request->getParameter('c');
        $username = $request->getParameter('uname');
        $password = $request->getParameter('pwd');
        $scode = $request->getParameter('scode');

        $this->valiAdminEntryCode($c);



        $settingService = Beans::get('system.setting.service');

        $verifyCodeEnable = $settingService->getSetting('verifyCode');

        Session::start();
        //是否开启验证码验证

        if($verifyCodeEnable == 'on'){
            if ( strtoupper($scode) != $_SESSION['scode'] ) {
                AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '验证码错误');
            }
        }



        $service = Beans::get($this->serviceBean);
        $user = $service->login($username, $password);
        if ( $user == false ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '登陆失败，用户名或者密码错误');
        }

        if ( $user['status'] == 0 ) {
            $service = Beans::get($this->serviceBean);
            $service->logout();
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '你的账户已被禁用，请联系管理员');
        }

        $request->addParameter('logmessage', "{$username} 登陆了系统");
        AjaxResult::ajaxSuccessResult();

    }

    //退出登陆
    public function logout(HttpRequest $request) {
        $service = Beans::get($this->serviceBean);
        $service->logout();
        location("/admin/login/index");
    }
}
